package lightswitch.server.code.object;

import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 
 * @author HieuPT
 * 
 */
public class Property {
	private String originName;
	private String name;// contain alias
	private String typeOfData;
	private int maxTypeLength;
	private boolean isPrimary;
	private boolean isAllowNull;
	public String alias="";
	// private String tbName;
	// private String colName;
	public final static String COLUMN_NAME = "NAME";
	public final static String COLUMN_TYPE = "TYPE";
	public final static String COLUMN_LENGTH = "LENGTH";
	public final static String COLUMN_IS_PRIMARY = "PRIMARY_KEY";
	public final static String COLUMN_ALLOW_NULL = "ALLOW_NULL";

	public static Property convertProperty(Property p, String aliasName) {
		Property result = p;
		String newName = aliasName + "." + p.getOriginName();
		result.setName(newName);
		return result;
	}

	public Property(String name, String typeOfData, int maxTypeLength) {
		this.name = name.toLowerCase();
		this.originName = this.name;
		this.typeOfData = typeOfData.toLowerCase();
		this.maxTypeLength = maxTypeLength;
		this.isPrimary = false;
		this.isAllowNull = false;
	}

	public Property(String name, String typeOfData, int maxTypeLength,
			boolean isPrimary, boolean isAllowNull) {
		this.name = name.toLowerCase();
		this.originName = this.name;
		this.typeOfData = typeOfData.toLowerCase();
		this.maxTypeLength = maxTypeLength;
		this.isPrimary = isPrimary;
		this.isAllowNull = isAllowNull;
	}

	public Property(Property pro) {
		originName = pro.originName;
		name = pro.name;// contain alias
		typeOfData = pro.typeOfData;
		maxTypeLength = pro.maxTypeLength;
		isPrimary = pro.isPrimary;
		isAllowNull = pro.isAllowNull;
	}

	public Property() {
		this.name = "null";
		this.originName = "null";
		this.typeOfData = "null";
		this.maxTypeLength = 0;
		this.isPrimary = false;
		this.isAllowNull = false;
	}

	public String getName() {
		return name;
	}

	public String getOriginName() {
		return originName;
	}

	public String getTypeOfData() {
		return typeOfData;
	}

	public int getMaxTypeLength() {
		return maxTypeLength;
	}

	public boolean getIsPrimary() {
		return isPrimary;
	}

	public boolean getIsAllowNull() {
		return isAllowNull;
	}

	// public String getTableName(){
	// return tbName;
	// }

	// public String getColName(){
	// return colName;
	// }
	// public void setTableName(String value){
	// tbName = value;
	// }
	// public void setColName(String value){
	// colName = value;
	// }

	public void setName(String value) {
		if (originName.equals(name) & !value.contains("."))
			originName = name = value.toLowerCase();
		else
			name = value.toLowerCase();
	}

	public void setTypeOfData(String value) {
		typeOfData = value.toLowerCase();
	}

	public void setMaxTypeLength(int value) {
		maxTypeLength = value;
	}

	public void setIsPrimary(boolean value) {
		isPrimary = value;
	}

	public void setIsAllowNull(boolean value) {
		isAllowNull = value;
	}

	public void setViaName(String name, Object value) {
		if (name.equals(COLUMN_NAME)) {
			setName(((String) value).toLowerCase());
		} else if (name.equals(COLUMN_TYPE)) {
			setTypeOfData(((String) value).toLowerCase());
		} else if (name.equals(COLUMN_LENGTH)) {
			setMaxTypeLength(Integer.parseInt((String) value));
		} else if (name.equals(COLUMN_IS_PRIMARY)) {
			setIsPrimary(Boolean.parseBoolean(((Boolean) value).toString()));
		} else if (name.equals(COLUMN_ALLOW_NULL)) {
			setIsAllowNull(Boolean.parseBoolean(((Boolean) value).toString()));
		}
	}

	public static Element createElement(Document doc, Property prop) {
		Element result = doc.createElement("PROPERTY");
		result.setAttribute("name", prop.getOriginName());
		result.setAttribute("type", prop.getTypeOfData());
		result.setAttribute("maxTypeLength", prop.getMaxTypeLengthToString());
		result.setAttribute("isPrimary", prop.getIsPrimary() ? "true" : "false");
		result.setAttribute("isAllowNull", prop.getIsAllowNull() ? "true"
				: "false");
		return result;
	}

	public String getMaxTypeLengthToString() {
		return Integer.toString(maxTypeLength);
	}

	public static Property getPropertyNull() {
		Property nullProp = new Property();
		nullProp.setName("null");
		return nullProp;
	}

	public static Property createPropertyFromNode(Element propertyElement) {
		String name = propertyElement.getAttribute("name");
		String type = propertyElement.getAttribute("type");
		int maxTypeLength = Integer.parseInt(propertyElement
				.getAttribute("maxTypeLength"));
		boolean isUnique = Boolean.parseBoolean(propertyElement
				.getAttribute("isPrimary"));
		boolean isCanNull = Boolean.parseBoolean(propertyElement
				.getAttribute("isAllowNull"));
		return new Property(name, type, maxTypeLength, isUnique, isCanNull);
	}

	public static Vector<String> toColumnNames() {
		Vector<String> result = new Vector<String>();
		result.add(COLUMN_NAME);
		result.add(COLUMN_TYPE);
		result.add(COLUMN_LENGTH);
		result.add(COLUMN_IS_PRIMARY);
		result.add(COLUMN_ALLOW_NULL);
		return result;
	}

	public static Vector<Object> toData(Property p) {
		Vector<Object> result = new Vector<Object>();
		result.add(p.getOriginName());
		result.add(p.getTypeOfData());
		result.add(p.getMaxTypeLengthToString());
		result.add(p.getIsPrimary());
		result.add(p.getIsAllowNull());
		return result;
	}

	public String toString() {
		return "(" + originName + "," + typeOfData + "(" + maxTypeLength + ")"
				+ "," + isPrimary + "," + isAllowNull + ")";
	}
}
